/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.ac.aptech.fat1.c0910g.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import vn.ac.aptech.fat1.c0910g.dao.FoodDAO;
import vn.ac.aptech.fat1.c0910g.dao.MenuDAO;
import vn.ac.aptech.fat1.c0910g.entity.Food;
import vn.ac.aptech.fat1.c0910g.entity.Menu;

/**
 *
 * @author NDC
 */
public class MenuDaoImpl implements MenuDAO {

    

    private Connection conn;

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public MenuDaoImpl() {
    }

    public MenuDaoImpl(Connection con) {
        this.conn = con;
    }

    public ArrayList<Menu> listMenu() throws Exception {
        try {
            ArrayList<Menu> listMenu = new ArrayList<Menu>();
            PreparedStatement stm = conn.prepareStatement("SELECT * FROM Menu");
            ResultSet rs = stm.executeQuery();

            while(rs.next()){
                Menu m = new Menu();
                m.setId(rs.getInt("menuId"));
                m.setName(rs.getString("name"));
                m.setDescription(rs.getString("description"));
                
                listMenu.add(m);
            }
            return listMenu;
        } catch (Exception e) {
            throw e;
        }
    }

    public boolean addMenu(Menu m) throws Exception {
        try {
            PreparedStatement stm = conn.prepareStatement("INSERT INTO Menu VALUES(?,?)");           
            stm.setString(1, m.getName());
            stm.setString(2, m.getDescription());

            int k = stm.executeUpdate();

            if(k>0){
                return true;
            }
            return false;
        } catch (Exception e) {
            throw e;
        }
    }

    public boolean updateMenu(Menu m) throws Exception {
        try {
            PreparedStatement stm = conn.prepareStatement("UPDATE Menu SET name=?,description=? WHERE menuId=?");
            stm.setString(1, m.getName());
            stm.setString(2, m.getDescription());
            stm.setInt(3, m.getId());
            
            int k = stm.executeUpdate();

            if(k>0){
                return true;
            }
            return false;
        } catch (Exception e) {
            throw e;
        }
    }

    public boolean addFoodToMenu(Menu m, Food f) throws Exception {
        try {
            PreparedStatement stm = conn.prepareStatement("INSERT INTO MenuItem VALUES(?,?)");
            stm.setInt(1, m.getId());
            stm.setInt(2, f.getId());
            int k = stm.executeUpdate();
            if(k>0){
                return true;
            }
            return false;
        } catch (Exception e) {
            throw e;
        }
    }

    public boolean removeFoodFromMenu(Menu m, Food f) throws Exception {
        try {
            PreparedStatement stm = conn.prepareStatement("DELETE FROM MenuItem WHERE menuId=? and foodId=?");
            stm.setInt(1, m.getId());
            stm.setInt(2, f.getId());

            int k = stm.executeUpdate();

            if(k>0){
                return true;
            }
            return false;
        } catch (Exception e) {
            throw e;
        }
    }

    public boolean deleteMenu(Menu m) throws Exception {
        try {
            PreparedStatement stm = conn.prepareStatement("DELETE FROM Menu WHERE menuId=?");
            stm.setInt(1, m.getId());

            int k = stm.executeUpdate();

            if(k>0){
                return true;
            }
            return false;
        } catch (Exception e) {
            throw e;
        }
    }

    public Menu getMenuById(int mid) throws Exception {
        try {
            Menu m = new Menu();
            FoodDAO fDAO = new FoodDaoImpl(conn);
            ArrayList<Food> list = new ArrayList<Food>();
            PreparedStatement stm = conn.prepareStatement("SELECT * FROM Menu WHERE menuId=?");
            stm.setInt(1, mid);
            PreparedStatement stmm = conn.prepareStatement("SELECT foodId FROM MenuItem WHERE menuId = ?");
            stmm.setInt(1, mid);
            ResultSet rs = stm.executeQuery();
            ResultSet rs1 = stmm.executeQuery();
            while(rs1.next()){
                list.add(fDAO.getFoodById(Integer.parseInt(rs1.getString("foodId"))));
            }
            if(rs.next()){
                m.setId(rs.getInt("menuId"));
                m.setName(rs.getString("name"));
                m.setDescription(rs.getString("description"));
                m.setFoods(list);
                return m;
            }

            return null;
        } catch (Exception e) {
            throw e;
        }
    }

    public boolean checkFood(Menu m, Food f) throws Exception {
        try {
            PreparedStatement stmm = conn.prepareStatement("SELECT foodId FROM MenuItem WHERE menuId = ? and foodId = ?");
            stmm.setInt(1, m.getId());
            stmm.setInt(2, f.getId());
            ResultSet rs = stmm.executeQuery();
            return rs.next();
        } catch (Exception e) {
            throw e;
        }
    }
}
